package com.hxkcloud.nurse.fragments;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.YjhApplication;
import com.hxkcloud.nurse.activitys.ArticleDetailActivity;
import com.hxkcloud.nurse.activitys.JoinDepartmentActivity;
import com.hxkcloud.nurse.activitys.MessageMainActivity;
import com.hxkcloud.nurse.activitys.NewsActivity;
import com.hxkcloud.nurse.activitys.QRcodeScanActivity;
import com.hxkcloud.nurse.adapter.DiscoverNDFragmentAdapter;
import com.hxkcloud.nurse.beans.DepartmentIndexInfo;
import com.hxkcloud.nurse.dborm.LoginInfoDao;
import com.hxkcloud.nurse.request.MessageCode;
import com.hxkcloud.nurse.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.nurse.utils.BrightnessUtils;
import com.hxkcloud.nurse.utils.ImageOptionUtils;
import com.hxkcloud.nurse.utils.ImageUtil;
import com.hxkcloud.nurse.utils.StringUtil;
import com.hxkcloud.nurse.views.CircleImageView;
import com.hxkcloud.nurse.views.RoundImageView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by william_sun on 2016-09-28.
 * Description:用户端发现模块(护士列表+医生列表)
 */
public class UserDiscoverFragment1 extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, View.OnClickListener {

    private View view;

    private LinearLayout ll_empty;//没有科室数据时显示，头部可以扫描二维码
    private LinearLayout ll_ttitle;
    private ImageView iv_qrcode;
    private TextView tv_title;
    private ImageView iv_tmessage;

    private LinearLayout ll_info;//展示科室信息

    private LinearLayout ll_title;
    private TextView tv_quit;
    private ImageView iv_message;
    private ImageView iv_msgpoint;
    private RelativeLayout rl_head;
    private CircleImageView civ_head;
    private ImageView iv_vip;
    private TextView tv_name;
    private LinearLayout ll_tags;

    private AppBarLayout appbar;
    private LinearLayout ll_departmentintro;
    //    private TextView tv_introup;
//    private ImageView iv_introup;
    private TextView tv_introduce;
    private LinearLayout ll_introdown;
    private LinearLayout ll_introup;

    private LinearLayout ll_course;
    private LinearLayout ll_course1;
    private LinearLayout ll_course2;
    private LinearLayout ll_course3;
    private LinearLayout ll_course4;
    private RelativeLayout rl_course1;//图像部分
    private RoundImageView iv_course1;//图像
    private TextView tv_course1readnum;//阅读数
    private TextView tv_course1title;//标题
    private TextView tv_course1intro;//简介
    private RelativeLayout rl_course2;//图像部分
    private RoundImageView iv_course2;//图像
    private TextView tv_course2readnum;//阅读数
    private TextView tv_course2title;//标题
    private TextView tv_course2intro;//简介
    private RelativeLayout rl_course3;//图像部分
    private RoundImageView iv_course3;//图像
    private TextView tv_course3readnum;//阅读数
    private TextView tv_course3title;//标题
    private TextView tv_course3intro;//简介
    private RelativeLayout rl_course4;//图像部分
    private RoundImageView iv_course4;//图像
    private TextView tv_course4readnum;//阅读数
    private TextView tv_course4title;//标题
    private TextView tv_course4intro;//简介

    private LinearLayout ll_news;
    private TextView tv_newsempty;
    private LinearLayout ll_news1;
    private LinearLayout ll_news2;
    private LinearLayout ll_news3;
    private View view_newsd1;
    private View view_newsd2;
    private RoundImageView iv_news1;
    private TextView tv_news1title;
    private TextView tv_news1date;
    private TextView tv_news1intro;
    private RoundImageView iv_news2;
    private TextView tv_news2title;
    private TextView tv_news2date;
    private TextView tv_news2intro;
    private RoundImageView iv_news3;
    private TextView tv_news3title;
    private TextView tv_news3date;
    private TextView tv_news3intro;

    private LinearLayout ll_nurse;
    private ImageView iv_nurse;
    private TextView tv_nurse;
    private View view_nurse;
    private LinearLayout ll_doctor;
    private ImageView iv_doctor;
    private TextView tv_doctor;
    private View view_doctor;
    private SwipeRefreshLayout swipeRefreshLayout;
    private ViewPager vp_nds;

    private FragmentManager fragmentManager;
    private DiscoverNDFragment nurseFragment;
    private DiscoverNDFragment doctorFragment;
    private DiscoverNDFragmentAdapter ndAdapter;
    private List<DiscoverNDFragment> ndFragments;

    private boolean introup = false;//是否展示介绍
    private int ndType = 0;//0护士 1医生

    private DepartmentIndexInfo departmentInfo;
    private int imgCorner = 0;

    private final int REQUEST_QRCODE = 500;
    private final int REQUEST_JOIN = 501;
    private String departmentId = "";

    //退出科室
    private PopupWindow popup_easynotice;
    private TextView tv_easynotice_title;
    private TextView tv_easynotice_cancel;
    private TextView tv_easynotice_sure;

    private TextView nodepartmentTV;

    private LoginInfoDao loginInfoDao;

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_GETDEPARTMENTINDEXINFO_SUCCESS:
                swipeRefreshLayout.setRefreshing(false);
                if (msg.obj != null) {
                    departmentInfo = (DepartmentIndexInfo) msg.obj;
                    showData();
                    return;
                } else {
                    toastMsg(R.string.departmentinfogetfail);
                }

                break;
            case MessageCode.NETWORKCODE_GETDEPARTMENTINDEXINFO_FAILURE:
                swipeRefreshLayout.setRefreshing(false);
                if (msg.obj != null) {
                    toastMsg((String) msg.obj);
                } else {
                    toastMsg(R.string.departmentinfogetfail);
                }

                break;
            case MessageCode.NETWORKCODE_DEPARTMENTLEFT_SUCCESS:
                toastMsg((String) msg.obj);
                ll_empty.setVisibility(View.VISIBLE);
                ll_info.setVisibility(View.GONE);

                loginInfo.setDepartmentId("");
                loginInfoDao.setUser(loginInfo);

                YjhApplication.msg_monitor = false;//监测提醒（医护）
                YjhApplication.msg_assessNurse = false;//评估提醒（医护）
                YjhApplication.msg_assessPatient = false;//评估提醒（患者）
                YjhApplication.msg_evalution = false;
                YjhApplication.msg_system = false;//系统消息
                YjhApplication.msg_chat = false;//环信聊天
                YjhApplication.msg_new = false;//全部判断

                if (idChangeListener != null) {
                    idChangeListener.onDepartmentIdChange();
                }
                break;
            case MessageCode.NETWORKCODE_DEPARTMENTLEFT_FAILURE:
                if (msg.obj != null) {
                    toastMsg((String) msg.obj);
                } else {
                    toastMsg(R.string.departmentquitfail);
                }
                break;
            case MessageCode.NETWORKCODE_DEPARTMENTJOIN_SUCCESS:
                if (idChangeListener != null) {
                    idChangeListener.onDepartmentIdChange();
                }
                toastMsg(R.string.departmentjoinsuccess);
                loginInfo.setDepartmentId(departmentId);
                loginInfoDao.setUser(loginInfo);

                initData();
                break;
            case MessageCode.NETWORKCODE_DEPARTMENTJOIN_FAILURE:
                if (msg.obj != null) {
                    toastMsg((String) msg.obj);
                } else {
                    toastMsg(R.string.departmentjoinfail);
                }
                break;
            case MessageCode.NETWORKCODE_CONNECT_ERROR:
                swipeRefreshLayout.setRefreshing(false);
                break;

        }
    }

    public void showPoint(boolean show) {
        if (iv_msgpoint != null)
            if (show) {
                iv_msgpoint.setVisibility(View.VISIBLE);
            } else {
                iv_msgpoint.setVisibility(View.INVISIBLE);
            }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_userdiscover1, container, false);

        initView();
        initPopup();
        initListener();
        return view;
    }

    @Override
    protected void initView() {
        ll_empty = (LinearLayout) view.findViewById(R.id.ll_discover_empty);
        ll_ttitle = (LinearLayout) view.findViewById(R.id.ll_title);
        iv_qrcode = (ImageView) view.findViewById(R.id.iv_title_back);
        tv_title = (TextView) view.findViewById(R.id.tv_title_title);
        iv_tmessage = (ImageView) view.findViewById(R.id.iv_title_func);

        ll_info = (LinearLayout) view.findViewById(R.id.ll_discover_info);

        ll_title = (LinearLayout) view.findViewById(R.id.ll_discover_title);
        tv_quit = (TextView) view.findViewById(R.id.tv_discover_quit);
        iv_message = (ImageView) view.findViewById(R.id.iv_discover_message);
        iv_msgpoint = (ImageView) view.findViewById(R.id.iv_discover_point);
        rl_head = (RelativeLayout) view.findViewById(R.id.rl_discover_head);
        civ_head = (CircleImageView) view.findViewById(R.id.civ_discover_head);
        iv_vip = (ImageView) view.findViewById(R.id.iv_discover_vip);
        tv_name = (TextView) view.findViewById(R.id.tv_discover_name);
        ll_tags = (LinearLayout) view.findViewById(R.id.ll_discover_tags);

        appbar = (AppBarLayout) view.findViewById(R.id.appbar_discover);
        ll_departmentintro = (LinearLayout) view.findViewById(R.id.ll_discover_departmentintro);
//        tv_introup = (TextView) view.findViewById(R.id.tv_discover_introup);
//        iv_introup = (ImageView) view.findViewById(R.id.iv_discover_introup);
        tv_introduce = (TextView) view.findViewById(R.id.tv_discover_indruduce);
        ll_introdown = (LinearLayout) view.findViewById(R.id.ll_discover_introdown);
        ll_introup = (LinearLayout) view.findViewById(R.id.ll_discover_introup);

        ll_course = (LinearLayout) view.findViewById(R.id.ll_discover_course);
        rl_course1 = (RelativeLayout) view.findViewById(R.id.rl_discover_course1);
        iv_course1 = (RoundImageView) view.findViewById(R.id.iv_discover_course1);
        tv_course1readnum = (TextView) view.findViewById(R.id.tv_discover_course1readnum);
        tv_course1title = (TextView) view.findViewById(R.id.tv_discover_course1title);
        tv_course1intro = (TextView) view.findViewById(R.id.tv_discover_course1intro);
        rl_course2 = (RelativeLayout) view.findViewById(R.id.rl_discover_course2);
        iv_course2 = (RoundImageView) view.findViewById(R.id.iv_discover_course2);
        tv_course2readnum = (TextView) view.findViewById(R.id.tv_discover_course2readnum);
        tv_course2title = (TextView) view.findViewById(R.id.tv_discover_course2title);
        tv_course2intro = (TextView) view.findViewById(R.id.tv_discover_course2intro);
        rl_course3 = (RelativeLayout) view.findViewById(R.id.rl_discover_course3);
        iv_course3 = (RoundImageView) view.findViewById(R.id.iv_discover_course3);
        tv_course3readnum = (TextView) view.findViewById(R.id.tv_discover_course3readnum);
        tv_course3title = (TextView) view.findViewById(R.id.tv_discover_course3title);
        tv_course3intro = (TextView) view.findViewById(R.id.tv_discover_course3intro);
        rl_course4 = (RelativeLayout) view.findViewById(R.id.rl_discover_course4);
        iv_course4 = (RoundImageView) view.findViewById(R.id.iv_discover_course4);
        tv_course4readnum = (TextView) view.findViewById(R.id.tv_discover_course4readnum);
        tv_course4title = (TextView) view.findViewById(R.id.tv_discover_course4title);
        tv_course4intro = (TextView) view.findViewById(R.id.tv_discover_course4intro);
        ll_course1 = (LinearLayout) view.findViewById(R.id.ll_discover_course1);
        ll_course2 = (LinearLayout) view.findViewById(R.id.ll_discover_course2);
        ll_course3 = (LinearLayout) view.findViewById(R.id.ll_discover_course3);
        ll_course4 = (LinearLayout) view.findViewById(R.id.ll_discover_course4);

        ll_news = (LinearLayout) view.findViewById(R.id.ll_discover_news);
        tv_newsempty = (TextView) view.findViewById(R.id.tv_discover_newsempty);
        ll_news1 = (LinearLayout) view.findViewById(R.id.ll_discover_news1);
        ll_news2 = (LinearLayout) view.findViewById(R.id.ll_discover_news2);
        ll_news3 = (LinearLayout) view.findViewById(R.id.ll_discover_news3);
        view_newsd1 = view.findViewById(R.id.view_discover_newsd1);
        view_newsd2 = view.findViewById(R.id.view_discover_newsd2);
        iv_news1 = (RoundImageView) view.findViewById(R.id.iv_discover_news1);
        tv_news1title = (TextView) view.findViewById(R.id.tv_discover_news1title);
        tv_news1date = (TextView) view.findViewById(R.id.tv_discover_news1date);
        tv_news1intro = (TextView) view.findViewById(R.id.tv_discover_news1intro);
        iv_news2 = (RoundImageView) view.findViewById(R.id.iv_discover_news2);
        tv_news2title = (TextView) view.findViewById(R.id.tv_discover_news2title);
        tv_news2date = (TextView) view.findViewById(R.id.tv_discover_news2date);
        tv_news2intro = (TextView) view.findViewById(R.id.tv_discover_news2intro);
        iv_news3 = (RoundImageView) view.findViewById(R.id.iv_discover_news3);
        tv_news3title = (TextView) view.findViewById(R.id.tv_discover_news3title);
        tv_news3date = (TextView) view.findViewById(R.id.tv_discover_news3date);
        tv_news3intro = (TextView) view.findViewById(R.id.tv_discover_news3intro);

        ll_nurse = (LinearLayout) view.findViewById(R.id.ll_discover_nurselist);
        tv_nurse = (TextView) view.findViewById(R.id.tv_discover_nurselist);
        iv_nurse = (ImageView) view.findViewById(R.id.iv_discover_nurselist);
        view_nurse = view.findViewById(R.id.view_discover_nurselist);
        ll_doctor = (LinearLayout) view.findViewById(R.id.ll_discover_doctorlist);
        tv_doctor = (TextView) view.findViewById(R.id.tv_discover_doctorlist);
        iv_doctor = (ImageView) view.findViewById(R.id.iv_discover_doctorlist);
        view_doctor = view.findViewById(R.id.view_discover_doctorlist);
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swiperefresh_discover);
        vp_nds = (ViewPager) view.findViewById(R.id.viewpager_discover_ndlist);

        nodepartmentTV = (TextView) view.findViewById(R.id.no_department_display);
        nodepartmentTV.setText(R.string.join_us);

        setTitlePadding(ll_ttitle);
        iv_qrcode.setImageResource(R.drawable.nav_scan);
        tv_title.setText(getResources().getString(R.string.homepage));
        iv_tmessage.setImageResource(R.drawable.nav_message);
        iv_qrcode.setVisibility(View.VISIBLE);
        iv_tmessage.setVisibility(View.VISIBLE);

        setTitlePadding(ll_title);

        LinearLayout.LayoutParams lp_headl = new LinearLayout.LayoutParams(screenWidth / 4, screenWidth / 4);
        rl_head.setLayoutParams(lp_headl);
        RelativeLayout.LayoutParams lp_head = new RelativeLayout.LayoutParams(screenWidth / 4, screenWidth / 4);
        civ_head.setLayoutParams(lp_head);
        RelativeLayout.LayoutParams lp_vip = new RelativeLayout.LayoutParams(screenWidth / 12, screenWidth / 12);
        lp_vip.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        lp_vip.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        lp_vip.rightMargin = ImageUtil.dip2px(5, mContext);
        lp_vip.bottomMargin = ImageUtil.dip2px(5, mContext);
        iv_vip.setLayoutParams(lp_vip);

        LinearLayout.LayoutParams lp_courserl = new LinearLayout.LayoutParams((screenWidth - ImageUtil.dip2px(45, mContext)) / 2, screenWidth / 4);
        rl_course1.setLayoutParams(lp_courserl);
        rl_course2.setLayoutParams(lp_courserl);
        rl_course3.setLayoutParams(lp_courserl);
        rl_course4.setLayoutParams(lp_courserl);

        ll_course4.setVisibility(View.VISIBLE);
        ll_course3.setVisibility(View.VISIBLE);
        ll_course2.setVisibility(View.VISIBLE);
        ll_course1.setVisibility(View.VISIBLE);//课程全展示，没有的显示默认的
        ll_news3.setVisibility(View.GONE);
        ll_news2.setVisibility(View.GONE);
        ll_news1.setVisibility(View.GONE);
        view_newsd1.setVisibility(View.GONE);
        view_newsd2.setVisibility(View.GONE);

        imgCorner = ImageUtil.dip2px(5, mContext);

        loginInfoDao = new LoginInfoDao(mContext);

        fragmentManager = getChildFragmentManager(); //((FragmentActivity) mContext).getSupportFragmentManager();
        nurseFragment = new DiscoverNDFragment(0);
        doctorFragment = new DiscoverNDFragment(1);
        ndFragments = new ArrayList<>();
        ndFragments.add(nurseFragment);
        ndFragments.add(doctorFragment);
        ndAdapter = new DiscoverNDFragmentAdapter(fragmentManager, ndFragments);
        vp_nds.setAdapter(ndAdapter);
        vp_nds.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    tv_nurse.setTextColor(ContextCompat.getColor(mContext, R.color.light_blue));
                    iv_nurse.setImageResource(R.drawable.nursehead_select);
                    view_nurse.setBackgroundColor(ContextCompat.getColor(mContext, R.color.light_blue));
                    tv_doctor.setTextColor(ContextCompat.getColor(mContext, R.color.black_text));
                    iv_doctor.setImageResource(R.drawable.doctorhead_unselect);
                    view_doctor.setBackgroundColor(ContextCompat.getColor(mContext, R.color.white));
                    ndType = 0;
                } else {
                    tv_nurse.setTextColor(ContextCompat.getColor(mContext, R.color.black_text));
                    iv_nurse.setImageResource(R.drawable.nursehead_unselect);
                    view_nurse.setBackgroundColor(ContextCompat.getColor(mContext, R.color.white));
                    tv_doctor.setTextColor(ContextCompat.getColor(mContext, R.color.light_blue));
                    iv_doctor.setImageResource(R.drawable.doctorhead_select);
                    view_doctor.setBackgroundColor(ContextCompat.getColor(mContext, R.color.light_blue));
                    ndType = 1;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        swipeRefreshLayout.setEnabled(true);
        swipeRefreshLayout.setOnRefreshListener(this);
        swipeRefreshLayout.setProgressViewOffset(true, -20, 100);
    }

    private void initPopup() {
        popup_easynotice = new PopupWindow(mContext);
        View viewNotice = LayoutInflater.from(mContext).inflate(R.layout.popup_easynotice, null);
        popup_easynotice.setContentView(viewNotice);

        popup_easynotice.setWidth((int) (screenWidth * 0.7));
        popup_easynotice.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popup_easynotice.setFocusable(true);
        popup_easynotice.setTouchable(true);
        popup_easynotice.setOutsideTouchable(true);
        popup_easynotice.setBackgroundDrawable(new ColorDrawable(0x00000000));
        popup_easynotice.setAnimationStyle(R.style.popupwindow_anim_alpha);

        tv_easynotice_title = (TextView) viewNotice.findViewById(R.id.tv_easynotice_title);
        tv_easynotice_cancel = (TextView) viewNotice.findViewById(R.id.tv_easynotice_cancel);
        tv_easynotice_sure = (TextView) viewNotice.findViewById(R.id.tv_easynotice_func);

        tv_easynotice_cancel.setOnClickListener(this);
        tv_easynotice_sure.setOnClickListener(this);

        tv_easynotice_title.setText(R.string.suretoquitdepartment);
        tv_easynotice_cancel.setText(R.string.cancel);
        tv_easynotice_sure.setText(R.string.sure);

        popup_easynotice.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                BrightnessUtils.lightOn(activity);
            }
        });
    }

    @Override
    protected void initListener() {
        iv_qrcode.setOnClickListener(this);
        iv_tmessage.setOnClickListener(this);
        iv_message.setOnClickListener(this);
        tv_quit.setVisibility(View.INVISIBLE);
//        tv_quit.setOnClickListener(this);
        ll_departmentintro.setOnClickListener(this);
        ll_introup.setOnClickListener(this);
        ll_course.setOnClickListener(this);
        ll_news.setOnClickListener(this);
        ll_course1.setOnClickListener(this);
        ll_course2.setOnClickListener(this);
        ll_course3.setOnClickListener(this);
        ll_course4.setOnClickListener(this);
        ll_news1.setOnClickListener(this);
        ll_news2.setOnClickListener(this);
        ll_news3.setOnClickListener(this);
        ll_nurse.setOnClickListener(this);
        ll_doctor.setOnClickListener(this);

        //appbar是否显示 的监听，用来控制列表是否可以刷新
        appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (ndAdapter != null) {
                    if (verticalOffset >= 0) {
//                        if (imageHandler != null)
//                            imageHandler.sendEmptyMessageDelayed(ImageHandler.MSG_UPDATE_IMAGE, ImageHandler.MSG_DELAY);
                        ndAdapter.getItem(ndType).setRefreshble(true, 0);

                    } else if (verticalOffset <= -appBarLayout.getMeasuredHeight()) {
//                        if (imageHandler != null)
//                            imageHandler.sendEmptyMessage(ImageHandler.MSG_KEEP_SILENT);
                        ndAdapter.getItem(ndType).setRefreshble(true, 1);
                    } else {
                        ndAdapter.getItem(ndType).setRefreshble(false, 0);
                    }
                }

                if (verticalOffset >= 0) {
                    swipeRefreshLayout.setEnabled(true);
                } else {
                    swipeRefreshLayout.setEnabled(false);
                }
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        if (TextUtils.isEmpty(loginInfo.getDepartmentId())) {
            ll_empty.setVisibility(View.VISIBLE);
            ll_info.setVisibility(View.GONE);
        } else {
            if (!TextUtils.isEmpty(loginInfo.getDepartmentId())) {
//                if (departmentInfo == null) {
                initData();
//                }
            }
            ll_empty.setVisibility(View.GONE);
            ll_info.setVisibility(View.VISIBLE);
        }

        if (YjhApplication.msg_new) {
            iv_msgpoint.setVisibility(View.VISIBLE);
        } else {
            iv_msgpoint.setVisibility(View.INVISIBLE);
        }
    }

    //个人信息页面加入科室后刷新数据
    public void setNewDepartment() {
        initData();
    }

    @Override
    protected void initData() {
        loading();
        YiJiaHuNetWorkUtils.getDepartmentIndexInfo(loginInfo.getDepartmentId(), handler);
    }

    private void showData() {
        if (departmentInfo != null) {
            imageLoader.displayImage(departmentInfo.getHeadImg(), civ_head, ImageOptionUtils.getInstance().getDepartmentHeadOptions());
            civ_head.setBorderColorResource(R.color.light_blue_border);
            civ_head.setBorderWidth(5);

            if (departmentInfo.getIsVip().equals("0")) {
                iv_vip.setVisibility(View.VISIBLE);
            } else {
                iv_vip.setVisibility(View.INVISIBLE);
            }
            tv_name.setText(departmentInfo.getTitle());
            LinearLayout.LayoutParams lp_tag = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            lp_tag.setMargins(5, 0, 5, 0);
            if (departmentInfo.getTags() != null && departmentInfo.getTags().size() > 0) {
                ll_tags.removeAllViews();
                for (int i = 0; i < Math.min(4, departmentInfo.getTags().size()); i++) {
                    String tag = departmentInfo.getTags().get(i);
                    TextView tv_tag1 = new TextView(mContext);
                    tv_tag1.setLayoutParams(lp_tag);
                    tv_tag1.setSingleLine(true);
                    tv_tag1.setMaxLines(1);
                    tv_tag1.setEllipsize(TextUtils.TruncateAt.END);
                    tv_tag1.setBackgroundResource(R.drawable.foundation_whitecorneralpha);
                    tv_tag1.setTextColor(Color.WHITE);
                    tv_tag1.setPadding(15, 10, 15, 10);
                    tv_tag1.setText(tag);
                    tv_tag1.setTextSize(12);
                    tv_tag1.setGravity(Gravity.CENTER);
                    ll_tags.addView(tv_tag1);
                }
            }
            tv_introduce.setText(StringUtil.ToDBC(departmentInfo.getIntro()));

            iv_course4.setImageResource(R.drawable.default_wenzhang);
            tv_course4readnum.setVisibility(View.GONE);
            tv_course4title.setText(R.string.updatingwait);
            tv_course4intro.setText("");
            iv_course3.setImageResource(R.drawable.default_wenzhang);
            tv_course3readnum.setVisibility(View.GONE);
            tv_course3title.setText(R.string.updatingwait);
            tv_course3intro.setText("");
            iv_course2.setImageResource(R.drawable.default_wenzhang);
            tv_course2readnum.setVisibility(View.GONE);
            tv_course2title.setText(R.string.updatingwait);
            tv_course2intro.setText("");
            iv_course1.setImageResource(R.drawable.default_wenzhang);
            tv_course1readnum.setVisibility(View.GONE);
            tv_course1title.setText(R.string.updatingwait);
            tv_course1intro.setText("");

            switch (Math.min(departmentInfo.getCourseList().size(), 4)) {
                case 4:
                    ll_course4.setVisibility(View.VISIBLE);
                    imageLoader.displayImage(departmentInfo.getCourseList().get(3).getImg(), iv_course4, ImageOptionUtils.getInstance().getArticlePicBigOptions(imgCorner));
                    tv_course4readnum.setText("阅读" + departmentInfo.getCourseList().get(3).getReadNum());
                    tv_course4readnum.setVisibility(View.VISIBLE);
                    tv_course4title.setText(departmentInfo.getCourseList().get(3).getName());
                    tv_course4intro.setText(departmentInfo.getCourseList().get(3).getIntro());
                case 3:
                    ll_course3.setVisibility(View.VISIBLE);
                    imageLoader.displayImage(departmentInfo.getCourseList().get(2).getImg(), iv_course3, ImageOptionUtils.getInstance().getArticlePicBigOptions(imgCorner));
                    tv_course3readnum.setText("阅读" + departmentInfo.getCourseList().get(2).getReadNum());
                    tv_course3readnum.setVisibility(View.VISIBLE);
                    tv_course3title.setText(departmentInfo.getCourseList().get(2).getName());
                    tv_course3intro.setText(departmentInfo.getCourseList().get(2).getIntro());
                case 2:
                    ll_course2.setVisibility(View.VISIBLE);
                    imageLoader.displayImage(departmentInfo.getCourseList().get(1).getImg(), iv_course2, ImageOptionUtils.getInstance().getArticlePicBigOptions(imgCorner));
                    tv_course2readnum.setText("阅读" + departmentInfo.getCourseList().get(1).getReadNum());
                    tv_course2readnum.setVisibility(View.VISIBLE);
                    tv_course2title.setText(departmentInfo.getCourseList().get(1).getName());
                    tv_course2intro.setText(departmentInfo.getCourseList().get(1).getIntro());
                case 1:
                    ll_course1.setVisibility(View.VISIBLE);
                    imageLoader.displayImage(departmentInfo.getCourseList().get(0).getImg(), iv_course1, ImageOptionUtils.getInstance().getArticlePicBigOptions(imgCorner));
                    tv_course1readnum.setText("阅读" + departmentInfo.getCourseList().get(0).getReadNum());
                    tv_course1readnum.setVisibility(View.VISIBLE);
                    tv_course1title.setText(departmentInfo.getCourseList().get(0).getName());
                    tv_course1intro.setText(departmentInfo.getCourseList().get(0).getIntro());
                    break;
            }

            SimpleDateFormat sdf = new SimpleDateFormat("yy/M/d");
            switch (Math.min(departmentInfo.getNewsList().size(), 3)) {
                case 3:
                    ll_news3.setVisibility(View.VISIBLE);
                    imageLoader.displayImage(departmentInfo.getNewsList().get(2).getImg(), iv_news3, ImageOptionUtils.getInstance().getArticlePicSmallOptions(imgCorner));
                    tv_news3title.setText(departmentInfo.getNewsList().get(2).getName());
                    String date3 = sdf.format(new Date(Long.valueOf(departmentInfo.getNewsList().get(2).getCreateDate())));
                    tv_news3date.setText(date3);
                    tv_news3intro.setText(departmentInfo.getNewsList().get(2).getIntro());
                    view_newsd2.setVisibility(View.VISIBLE);
                case 2:
                    ll_news2.setVisibility(View.VISIBLE);
                    imageLoader.displayImage(departmentInfo.getNewsList().get(1).getImg(), iv_news2, ImageOptionUtils.getInstance().getArticlePicSmallOptions(imgCorner));
                    tv_news2title.setText(departmentInfo.getNewsList().get(1).getName());
                    String date2 = sdf.format(new Date(Long.valueOf(departmentInfo.getNewsList().get(1).getCreateDate())));
                    tv_news2date.setText(date2);
                    tv_news2intro.setText(departmentInfo.getNewsList().get(1).getIntro());
                    view_newsd1.setVisibility(View.VISIBLE);
                case 1:
                    ll_news1.setVisibility(View.VISIBLE);
                    imageLoader.displayImage(departmentInfo.getNewsList().get(0).getImg(), iv_news1, ImageOptionUtils.getInstance().getArticlePicSmallOptions(imgCorner));
                    tv_news1title.setText(departmentInfo.getNewsList().get(0).getName());
                    String date1 = sdf.format(new Date(Long.valueOf(departmentInfo.getNewsList().get(0).getCreateDate())));
                    tv_news1date.setText(date1);
                    tv_news1intro.setText(departmentInfo.getNewsList().get(0).getIntro());
                    break;
                case 0:
                    tv_newsempty.setVisibility(View.VISIBLE);
                    break;
            }

        }
    }

    @Override
    public void onRefresh() {
        loading();
        YiJiaHuNetWorkUtils.getDepartmentIndexInfo(loginInfo.getDepartmentId(), handler);
        if (vp_nds.getCurrentItem() == 0) {
            nurseFragment.refresh();
        }
        if (vp_nds.getCurrentItem() == 1) {
            doctorFragment.refresh();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back:
                Intent iQR = new Intent(mContext, QRcodeScanActivity.class);
                startActivityForResult(iQR, REQUEST_QRCODE);
                break;
            case R.id.iv_title_func:
            case R.id.iv_discover_message:
                if (!TextUtils.isEmpty(loginInfo.getDepartmentId())) {
                    Intent iM = new Intent(mContext, MessageMainActivity.class);
                    startActivity(iM);
                } else {
                    toastMsg(R.string.pleasefirstjoindepartment);
                }
                break;
            case R.id.tv_discover_quit:
                popup_easynotice.showAtLocation(ll_title, Gravity.CENTER, 0, 0);
                BrightnessUtils.lightOff(activity);
                break;
            case R.id.ll_discover_departmentintro:
            case R.id.ll_discover_introup:
                if (introup) {
//                    tv_introup.setText(R.string.packdown);
//                    iv_introup.setImageResource(R.drawable.list_unfold);
                    ll_introdown.setVisibility(View.VISIBLE);
                    ll_introup.setVisibility(View.GONE);
                    tv_introduce.setVisibility(View.VISIBLE);
                    tv_introduce.setSingleLine(true);
                    introup = false;
                } else {
//                    tv_introup.setText(R.string.packup);
//                    iv_introup.setImageResource(R.drawable.list_unfold_pre);
                    ll_introdown.setVisibility(View.GONE);
                    ll_introup.setVisibility(View.VISIBLE);
                    tv_introduce.setVisibility(View.VISIBLE);
                    tv_introduce.setSingleLine(false);
                    introup = true;
                }
                break;
            case R.id.ll_discover_news:
                Intent newsIntent = new Intent(getActivity(), NewsActivity.class);
                newsIntent.putExtra("type", YjhApplication.articlekind_news);
                startActivity(newsIntent);
                break;
            case R.id.ll_discover_course:
                Intent courseIntent = new Intent(getActivity(), NewsActivity.class);
                courseIntent.putExtra("type", YjhApplication.articlekind_course);
                startActivity(courseIntent);
                break;
            case R.id.tv_easynotice_cancel:
                popup_easynotice.dismiss();
                break;
            case R.id.tv_easynotice_func:
                popup_easynotice.dismiss();
                loading();
                YiJiaHuNetWorkUtils.departmentLeft(loginInfo.getUserId(), handler);
                break;
            case R.id.ll_discover_course1:
                if (departmentInfo==null)
                    break;
                if (departmentInfo.getCourseList().size() > 0) {
                    Intent iArticle = new Intent(mContext, ArticleDetailActivity.class);
                    iArticle.putExtra("articleId", departmentInfo.getCourseList().get(0).getItemId());
                    iArticle.putExtra("acctType", YjhApplication.articlekind_course);
                    iArticle.putExtra("name", departmentInfo.getCourseList().get(0).getName());
                    iArticle.putExtra("kindTitle", departmentInfo.getCourseList().get(0).getKindName());
                    startActivity(iArticle);
                }
                break;
            case R.id.ll_discover_course2:
                if (departmentInfo==null)
                    break;
                if (departmentInfo.getCourseList().size() > 1) {
                    Intent iArticle = new Intent(mContext, ArticleDetailActivity.class);
                    iArticle.putExtra("articleId", departmentInfo.getCourseList().get(1).getItemId());
                    iArticle.putExtra("acctType", YjhApplication.articlekind_course);
                    iArticle.putExtra("name", departmentInfo.getCourseList().get(1).getName());
                    iArticle.putExtra("kindTitle", departmentInfo.getCourseList().get(1).getKindName());
                    startActivity(iArticle);
                }
                break;
            case R.id.ll_discover_course3:
                if (departmentInfo==null)
                    break;
                if (departmentInfo.getCourseList().size() > 2) {
                    Intent iArticle = new Intent(mContext, ArticleDetailActivity.class);
                    iArticle.putExtra("articleId", departmentInfo.getCourseList().get(2).getItemId());
                    iArticle.putExtra("acctType", YjhApplication.articlekind_course);
                    iArticle.putExtra("name", departmentInfo.getCourseList().get(2).getName());
                    iArticle.putExtra("kindTitle", departmentInfo.getCourseList().get(2).getKindName());
                    startActivity(iArticle);
                }
                break;
            case R.id.ll_discover_course4:
                if (departmentInfo==null)
                    break;
                if (departmentInfo.getCourseList().size() > 3) {
                    Intent iArticle = new Intent(mContext, ArticleDetailActivity.class);
                    iArticle.putExtra("articleId", departmentInfo.getCourseList().get(3).getItemId());
                    iArticle.putExtra("acctType", YjhApplication.articlekind_course);
                    iArticle.putExtra("name", departmentInfo.getCourseList().get(3).getName());
                    iArticle.putExtra("kindTitle", departmentInfo.getCourseList().get(3).getKindName());
                    startActivity(iArticle);
                }
                break;
            case R.id.ll_discover_news1:
                if (departmentInfo==null)
                    break;
                if (departmentInfo.getNewsList().size() > 0) {
                    Intent iArticle = new Intent(mContext, ArticleDetailActivity.class);
                    iArticle.putExtra("articleId", departmentInfo.getNewsList().get(0).getItemId());
                    iArticle.putExtra("acctType", YjhApplication.articlekind_news);
                    iArticle.putExtra("name", departmentInfo.getNewsList().get(0).getName());
                    iArticle.putExtra("kindTitle", departmentInfo.getNewsList().get(0).getKindName());
                    startActivity(iArticle);
                }
                break;
            case R.id.ll_discover_news2:
                if (departmentInfo.getNewsList().size() > 1) {
                    Intent iArticle = new Intent(mContext, ArticleDetailActivity.class);
                    iArticle.putExtra("articleId", departmentInfo.getNewsList().get(1).getItemId());
                    iArticle.putExtra("acctType", YjhApplication.articlekind_news);
                    iArticle.putExtra("name", departmentInfo.getNewsList().get(1).getName());
                    iArticle.putExtra("kindTitle", departmentInfo.getNewsList().get(1).getKindName());
                    startActivity(iArticle);
                }
                break;
            case R.id.ll_discover_news3:
                if (departmentInfo.getNewsList().size() > 2) {
                    Intent iArticle = new Intent(mContext, ArticleDetailActivity.class);
                    iArticle.putExtra("articleId", departmentInfo.getNewsList().get(2).getItemId());
                    iArticle.putExtra("acctType", YjhApplication.articlekind_news);
                    iArticle.putExtra("name", departmentInfo.getNewsList().get(2).getName());
                    iArticle.putExtra("kindTitle", departmentInfo.getNewsList().get(2).getKindName());
                    startActivity(iArticle);
                }
                break;
            case R.id.ll_discover_nurselist:
                if (ndType == 1) {
                    tv_nurse.setTextColor(ContextCompat.getColor(mContext, R.color.light_blue));
                    iv_nurse.setImageResource(R.drawable.nursehead_select);
                    view_nurse.setBackgroundColor(ContextCompat.getColor(mContext, R.color.light_blue));
                    tv_doctor.setTextColor(ContextCompat.getColor(mContext, R.color.black_text));
                    iv_doctor.setImageResource(R.drawable.doctorhead_unselect);
                    view_doctor.setBackgroundColor(ContextCompat.getColor(mContext, R.color.white));
                    vp_nds.setCurrentItem(0);
                    ndType = 0;
                }
                break;
            case R.id.ll_discover_doctorlist:
                if (ndType == 0) {
                    tv_nurse.setTextColor(ContextCompat.getColor(mContext, R.color.black_text));
                    iv_nurse.setImageResource(R.drawable.nursehead_unselect);
                    view_nurse.setBackgroundColor(ContextCompat.getColor(mContext, R.color.white));
                    tv_doctor.setTextColor(ContextCompat.getColor(mContext, R.color.light_blue));
                    iv_doctor.setImageResource(R.drawable.doctorhead_select);
                    view_doctor.setBackgroundColor(ContextCompat.getColor(mContext, R.color.light_blue));
                    vp_nds.setCurrentItem(1);
                    ndType = 1;
                }
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == REQUEST_QRCODE) {
                departmentId = data.getStringExtra("result");

                if (!TextUtils.isEmpty(departmentId)) {
                    Intent iIntro = new Intent(mContext, JoinDepartmentActivity.class);
                    iIntro.putExtra("departmentId", departmentId);
                    startActivityForResult(iIntro, REQUEST_JOIN);

//                    loading();
//                    YiJiaHuNetWorkUtils.departmentJoin(departmentId, loginInfo.getUserId(), handler);
                }
            } else if (requestCode == REQUEST_JOIN) {
                boolean isJoin = data.getBooleanExtra("join", false);
                if (isJoin) {
//                    loading();
//                    YiJiaHuNetWorkUtils.departmentJoin(departmentId, loginInfo.getUserId(), handler);
                    if (idChangeListener != null) {
                        idChangeListener.onDepartmentIdChange();
                    }
                    loginInfo.setDepartmentId(departmentId);
                    loginInfoDao.setUser(loginInfo);
                    toastMsg(R.string.departmentjoinsuccess);
                    initData();
                } else {

                }
            }
        }
    }

    private UserDiscoverFragment.departmentIdChangeListener idChangeListener;

    public void setDepartmentIdChangeListener(UserDiscoverFragment.departmentIdChangeListener listener) {
        this.idChangeListener = listener;
    }
}
